package mju.cxf.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import mju.cxf.constant.MessageConstant;
import mju.cxf.dto.EmployeeDTO;
import mju.cxf.dto.EmployeeLoginDTO;
import mju.cxf.dto.ResetPasswordDTO;
import mju.cxf.entity.Admin;
import mju.cxf.entity.Categories;
import mju.cxf.entity.Employee;
import mju.cxf.exception.LoginFailedException;
import mju.cxf.mapper.EmployeeMapper;
import mju.cxf.service.EmployeeService;
import mju.cxf.utils.SHA256Util;
import mju.cxf.utils.TimestampUpdaterUtils;
import mju.cxf.vo.CategoriesVO;
import mju.cxf.vo.EmployeeVO;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

/**
 * <p>
 * 员工表 服务实现类
 * </p>
 *
 * @author 郑献
 * @since 2024-10-13
 */

@Service
public class EmployeeServiceImpl extends ServiceImpl<EmployeeMapper,Employee> implements EmployeeService  {
    /**
     * A分页查询员工信息
     * @param name
     * @return
     */
    @Override
    public Page<EmployeeDTO> selectPageByName(String empNumber, String name, int current, int size) {
        Page<EmployeeDTO> page = new Page<>(current, size);
        return this.baseMapper.selectPageVo(page, empNumber, name);
    }

    /**
     * A新增员工信息
     * @param employeeDTO
     * @return
     */
    @Override
    public boolean saveEmployee(EmployeeDTO employeeDTO) {
        Employee employee = new Employee();
        employeeDTO.setCreateTime(LocalDateTime.now());
        BeanUtils.copyProperties(employeeDTO, employee);
        TimestampUpdaterUtils.saveUtil(employee);
        return this.save(employee); // 使用 MyBatis-Plus 提供的 save() 方法
    }

    /**
     * E修改员工信息
     * @param employeeDTO
     * @return
     */
    @Override
    public boolean updateEmployee(EmployeeDTO employeeDTO) {
        Employee employee = new Employee();
        BeanUtils.copyProperties(employeeDTO, employee);
        employee.setDeleted(0);
        TimestampUpdaterUtils.updateUtil(employee);
        return baseMapper.updateById(employee) == 1;
    }

    /**
     * A删除员工信息
     * @param id
     * @return
     */
    @Override
    public boolean deleteEmployee(Integer id) {
        return this.removeById(id);
    }

    /**
     * A批量删除员工信息
     * @param ids
     * @return
     */
    @Override
    public boolean deleteEmployees(List<Integer> ids) {
        return this.removeByIds(ids);
    }

    /**
     * E员工登陆
     * @param employeeLoginDTO
     * @return
     */
    @Override
    public Employee loginEmployee(EmployeeLoginDTO employeeLoginDTO) {
        Employee employee = new Employee();
        BeanUtils.copyProperties(employeeLoginDTO,employee);
        LambdaQueryWrapper<Employee> lqw = new LambdaQueryWrapper<>();
        lqw.eq(Employee::getEmpNumber,employee.getEmpNumber());
        Employee employeeByLogin = baseMapper.selectOne(lqw);
        if (employeeByLogin != null){
            String encrypt = SHA256Util.encrypt(employee.getPassword());
            //判断密码校验是否成功
            if (encrypt.equals(employeeByLogin.getPassword())){
                return employeeByLogin;
            }else{
                throw new LoginFailedException(MessageConstant.PASSWORD_ERROR);
            }
        }else{
            throw new LoginFailedException(MessageConstant.ACCOUNT_NOT_FOUND);
        }
    }

    /**
     * A修改员工密码
     * @param resetPasswordDTO
     * @return
     */
    @Override
    public boolean updatePassword(ResetPasswordDTO resetPasswordDTO) {
        LambdaQueryWrapper<Employee> lqw=new LambdaQueryWrapper<>();
        lqw.eq(Employee::getId, resetPasswordDTO.getId());
        Employee employee = baseMapper.selectOne(lqw);

        //密码加密
        String encrypt = SHA256Util.encrypt(resetPasswordDTO.getPassword());
        String newEncrypt = SHA256Util.encrypt(resetPasswordDTO.getNewPassword());

        if(encrypt.equals(employee.getPassword())){
            employee.setPassword(newEncrypt);
            this.updateById(employee);
            return true;
        }
        return false;
    }
}
